package jmine.tec.script.persistent.repository;

import jmine.tec.script.ScriptExecutor;
import jmine.tec.script.persistent.dao.ScriptLanguageIdentifier;
import jmine.tec.script.persistent.domain.ScriptLanguage;
import jmine.tec.test.Builder;

/**
 * Builder de ScriptLanguage
 * 
 * @author lundberg
 */
public class ScriptLanguageBuilder implements Builder<ScriptLanguage>, Cloneable {

    private ScriptLanguageIdentifier identifier;

    private String mnemonico;

    private String abreviacao;

    private Class<? extends ScriptExecutor> executorClass;

    /**
     * Construtor
     */
    public ScriptLanguageBuilder() {
        // OK
    }

    public static ScriptLanguageBuilder aLanguage() {
        return new ScriptLanguageBuilder();
    }

    public ScriptLanguageBuilder identifiedBy(ScriptLanguageIdentifier identifier) {
        ScriptLanguageBuilder copy = this.copy();
        copy.identifier = identifier;
        return copy;
    }

    public ScriptLanguageBuilder comMnemonico(String mnemonico) {
        ScriptLanguageBuilder copy = this.copy();
        copy.mnemonico = mnemonico;
        return copy;
    }

    public ScriptLanguageBuilder comAbreviacao(String abreviacao) {
        ScriptLanguageBuilder copy = this.copy();
        copy.abreviacao = abreviacao;
        return copy;
    }

    public ScriptLanguageBuilder executadoPor(Class<? extends ScriptExecutor> executorClass) {
        ScriptLanguageBuilder copy = this.copy();
        copy.executorClass = executorClass;
        return copy;
    }

    /**
     * {@inheritDoc}
     */
    public ScriptLanguage build() {
        ScriptLanguage language = new ScriptLanguage();
        language.setId(this.identifier.getSystemId());
        language.setMnemonico(this.mnemonico);
        language.setAbreviacao(this.abreviacao);
        language.setExecutorClass(this.executorClass.getCanonicalName());
        return language;
    }

    private ScriptLanguageBuilder copy() {
        try {
            return (ScriptLanguageBuilder) this.clone();
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException(e);
        }
    }
}
